const pad = (n) => n < 10 ? '0' + n : n;

// 2019-03-09
const loc_day = (v) => v ? pad(v.getFullYear()) + "-" + pad(v.getMonth() + 1) + "-" + pad(v.getDate()) : null;

// 20:33:09
const loc_time = (v) => v ? pad(v.getHours()) + ":" + pad(v.getMinutes()) + ":" + pad(v.getSeconds()) : null;

// 20:33
// 2019-03-09T20:33:09Z
// 20190309203309
const get_tag = (v) => `${pad(v.getFullYear())}${ pad(v.getMonth() + 1)}${pad(v.getDate())}${pad(v.getHours())}${pad(v.getMinutes())}${pad(v.getSeconds())}`;
const loc_tag = () => get_tag(new Date());

// 2019-03-09 20:33:09
const loc_dt = (v) => v ? loc_day(v) + " " + loc_time(v) : null;

// 2019-03-09T20:33:09Z
const loc_date = () => {
    const v = new Date();

    return loc_day(v) + "T" + loc_time(v) + "Z";
};

// s + 8 hours
// 2019-03-09 20:33:09
const get_week = (i) => "星期" + "日一二三四五六".charAt(i);

// ["2019-03-09", "20:33:09", "星期一"]
const loc_week = () => {
    const v = new Date();
    return [loc_day(v), loc_time(v), get_week(v.getDay())];
};

const loc_tm = (v) => v ? pad(v.getHours()) + ":" + pad(v.getMinutes()) + ":" + pad(v.getSeconds()) + "." + v.getMilliseconds() : null;

const ge_date = (d=30) => {
    let v = new Date();
    v.setDate(v.getDate() - d);

    return `${loc_day(v)}T00:00:00Z`;
};

// 2019-03-09
const utc_day = (v) => v ? pad(v.getUTCFullYear()) + "-" + pad(v.getUTCMonth() + 1) + "-" + pad(v.getUTCDate()) : null;

// 20:33:09
const utc_time = (v) => v ? pad(v.getUTCHours()) + ":" + pad(v.getUTCMinutes()) + ":" + pad(v.getUTCSeconds()) : null;

// s + 8 hours
// 2019-03-09 20:33:09
const utc_8 = (s, t=8) => {
    let v = new Date(s);
    v.setTime(v.getTime() + t*60*60*1000);
    return utc_day(v) + " " + utc_time(v);
};

export {utc_8, ge_date, loc_date, loc_dt, loc_tm, loc_tag, loc_week};
